package cn.net.xinyi.xs.view {
	
	import cn.net.xinyi.xs.RamsFacade;
	import cn.net.xinyi.xs.model.LoadOrgansProxy;
	import cn.net.xinyi.xs.view.components.OrganList;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class OrgansListMediator extends Mediator {
		
		public static const NAME:String = 'OrgansDataGridMediator';
		
		public function OrgansListMediator(viewComponent:OrganList) {
			super(NAME, viewComponent);
			organList.addEventListener(OrganList.LOAD_ORGANS, onLoadOrgans);
			organList.addEventListener(OrganList.TO_FIRSTPAGE, onLoadOrgans);
			organList.addEventListener(OrganList.TO_ENDPAGE, onLoadOrgans);
			organList.addEventListener(OrganList.TO_PREPAGE, onLoadOrgans);
			organList.addEventListener(OrganList.TO_NEXTPAGE, onLoadOrgans);
		}
		
		protected function onLoadOrgans(event:Event):void {
			sendNotification(RamsFacade.LOAD_ORGANS, organList.pageVO);
		}
		
		override public function listNotificationInterests():Array {
			return [
				LoadOrgansProxy.LOAD_ORGANCOUNT_SUCCESS,
				LoadOrgansProxy.LOAD_ORGANS_SUCCESS,
				LoadOrgansProxy.LOAD_ORGANS_FAILED
			];
		}
		
		override public function handleNotification(note:INotification):void {
			switch (note.getName()) {
				case LoadOrgansProxy.LOAD_ORGANCOUNT_SUCCESS:
					 organList.pageVO.resultSize = note.getBody() as int;
					 organList.pageVO.totalPage = Math.floor((organList.pageVO.resultSize + organList.pageVO.pageSize - 1) / organList.pageVO.pageSize);
					 break;
				case LoadOrgansProxy.LOAD_ORGANS_SUCCESS:
					 organList.organsList.dataProvider = note.getBody();
					 CursorManager.removeBusyCursor();
					 break;
				case LoadOrgansProxy.LOAD_ORGANS_FAILED:
					 trace(note.getBody().toString());
					 Alert.show("服务器响应错误，请与管理员联系！", "Error");
					 break;
			}
		}
		
		protected function get organList():OrganList {
			return viewComponent as OrganList;
		}
	}
}